﻿using System;
using System.Collections.Generic;
using System.Text;
using hedefgrup.reports.renault.date;
using hedefgrup.reports.common.SSRD;

namespace hedefgrup.reports.common
{
    public class ReportResult
    {
        public Report_Abstract Report;
        public List<Row> HeaderRows = new List<Row>();
        internal List<Row> Rows = new List<Row>();

        public ReportResult(Report_Abstract report)
        {
            Report = report;
        }

        private string ToFilterText(string currentFilter)
        {
            hedefgrup.reports.common.Tree question = Report.Context.Questionnaire.GetChild(currentFilter);
            if (question != null)
            {
                return currentFilter + ": " + question.Text;
            }
            else
            {
                return currentFilter;
            }
        }

        public string Dump()
        {
            StringBuilder reportBuilder = new StringBuilder();
            reportBuilder.AppendLine(Report.DisplayName);


            Criteria ssmPeriodCriteria = Report.GetCriteriaByName("SSM Period");
            if (ssmPeriodCriteria != null && ssmPeriodCriteria.Value != Criteria_SSRD_SSMPeriod.PleaseSelect)
            {
                reportBuilder.AppendFormat("SSM Periyodu:\t{0}", ssmPeriodCriteria.Value);
            }
            else
            {
                Criteria beginMonthCriteria = Report.GetCriteriaByName("Begin Month");
                if (beginMonthCriteria != null)
                {
                    reportMonth beginMonth = new reportMonth(beginMonthCriteria.Value);
                    reportBuilder.AppendFormat("Başlangıç:\t-{0} {1}-", beginMonth.getMonthName(), beginMonth.getYear());
                }

                Criteria endMonthCriteria = Report.GetCriteriaByName("End Month");
                if (endMonthCriteria != null)
                {
                    reportMonth endMonth = new reportMonth(endMonthCriteria.Value);
                    reportBuilder.AppendFormat("{0}:\t-{1} {2}-", beginMonthCriteria != null ? "\tBitiş" : "Raporlanan Ay", endMonth.getMonthName(), endMonth.getYear());
                }

                Criteria rollCriteria = Report.GetCriteriaByName("Roll");
                if (rollCriteria != null)
                {
                    reportBuilder.AppendFormat("\tRoll:\t{0}", rollCriteria.Value);
                }
            }

            Criteria networkCriteria = Report.GetCriteriaByName("Network");
            if (networkCriteria != null)
            {
                reportBuilder.AppendFormat("\tŞebeke:\t{0}", networkCriteria.Value);
            }

            Criteria oganizationCriteria = Report.GetCriteriaByName("Organization");
            if (oganizationCriteria != null)
            {
                reportBuilder.AppendFormat("\tMarka:\t{0}", oganizationCriteria.Value);
            }

            reportBuilder.AppendLine();

            Filter[] appliedFilters = Report.GetAppliedFilters();
            if (appliedFilters.Length > 0)
            {
                reportBuilder.AppendLine("Filtreler:");
                foreach (Filter appliedFilter in appliedFilters)
                {
                    hedefgrup.reports.common.Tree question = Report.Context.Questionnaire.GetChild(appliedFilter.Name);
                    foreach (string value in appliedFilter.Values)
                    {
                        if (question != null)
                        {
                            hedefgrup.reports.common.Tree option = question.GetChild(value);
                            if (option != null)
                            {
                                reportBuilder.AppendFormat("{0}\t{1}\r\n", ToFilterText(appliedFilter.Name), option.Key + ": " + option.Text);
                            }
                            else
                            {
                                reportBuilder.AppendFormat("{0}\t{1}\r\n", ToFilterText(appliedFilter.Name), value);
                            }
                        }
                        else
                        {
                            reportBuilder.AppendFormat("{0}\t{1}\r\n", ToFilterText(appliedFilter.Name), value);
                        }
                    }
                }
            }

            reportBuilder.AppendLine();

            if (HeaderRows.Count > 0)
            {
                foreach (Row headerRow in HeaderRows)
                {
                    foreach (Cell headerCell in headerRow.Cells)
                    {
                        reportBuilder.AppendFormat("{0}\t", headerCell.FullText);
                        if (headerCell.Span > 1)
                        {
                            for (int i = 0; i < headerCell.Span - 1; i++)
                            {
                                reportBuilder.Append("\t");
                            }
                        }
                    }
                    reportBuilder.AppendLine();
                }
            }

            foreach (Row reportRow in GetRows())
            {
                foreach (Cell dataCell in reportRow.Cells)
                {
                    reportBuilder.AppendFormat("{0}\t", dataCell.FullText);
                    if (dataCell.Span > 1)
                    {
                        for (int i = 0; i < dataCell.Span - 1; i++)
                        {
                            reportBuilder.Append("\t");
                        }
                    }
                }
                reportBuilder.AppendLine();
            }

            return reportBuilder.ToString();
        }

        public IEnumerable<Row> GetRows()
        {
            if (user.Current == null || user.Current.getDealerCodes() == null || user.Current.getDealerCodes().Length == 0)
            {
                return Rows;
            }

            List<string> authorizedDealerCodes = Static.GetAuthorizedDealers(Report);

            if (authorizedDealerCodes == null || authorizedDealerCodes.Count == 0)
            {
                return Rows;
            }

            List<Row> filteredRows = new List<Row>();

            foreach (Row row in Rows)
            {
                bool authorized = true;
                foreach (Cell cell in row.Cells)
                {
                    if (cell is DealerCodeCell)
                    {
                        if (!authorizedDealerCodes.Contains(cell.Text))
                        {
                            authorized = false;
                            break;
                        }
                    }
                }
                if (authorized)
                {
                    filteredRows.Add(row);
                }
            }

            return filteredRows;
        }
    }
}
